const {
  $util,
} = getApp();
Page({
  data: {
    money: '',
    userInfo: null,
    selectedClass: '',
  },
  onLoad(ops) {
    this.loadDetail();
    wx.setNavigationBarTitle({
      title: ops.barTitle && decodeURIComponent(ops.barTitle) || '捐赠',
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
  },
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/partyman.donation.index',
      data: {},
      success: (res) => {
        console.log(res);
        this.setData({
          class: res.dcatgory,
          userInfo: res.info,
        });
      },
    });
  },
  selectClass(e) {
    this.setData({
      selectedClass: this.data.class[e.detail.value],
    });
  },
  setMoney(e) {
    this.setData({
      money: e.detail.value,
    });
  },
  payment() {
    const D = this.data;
    if (!D.selectedClass.cid) {
      wx.showToast({
        title: '请先选择捐赠类目',
        icon: 'none',
      });
      return;
    }
    let m = D.money.split('.');
    if (m.length > 1 && m[1].length > 2) {
      wx.showToast({
        title: '捐赠金额的最小单位不能小于0.01',
        icon: 'none',
      });
      return;
    }
    $util.request({
      url: 'entry/wxapp/partyman.donation.donationpay',
      data: {
        cid: D.selectedClass.cid,
        fee: D.money,
      },
      success: (res) => {
        const {
          nonceStr,
          paySign,
          signType,
          timeStamp,
        } = res.payinfo;
        wx.requestPayment({
          timeStamp,
          nonceStr,
          package: res.payinfo.package,
          paySign,
          signType,
          success: (res) => {
            wx.showToast({
              title: '捐赠成功',
              duration: 2e3,
            });
            this.setData({
              money: '',
            });
          },
        });
      },
    });
  },
});
